#include "mediacontrol.h"

MediaControl::MediaControl(QWidget *parent)
{
    locatonList = new QMediaPlaylist(this);
    FTPList = new QMediaPlaylist(this);
    this->setPlaylist(locatonList);
}

void MediaControl::setPlaylist_locaton()
{
    this->setPlaylist(locatonList);
}
void MediaControl::setPlaylist_FTP()
{
    this->setPlaylist(FTPList);
}

void MediaControl::setMeidaPath(QString path)
{
    mediaPath = path;
}

void MediaControl::setMeidaName(QString name)
{
    mediaName = name;
}

void MediaControl::playMedia(void)
{
    locatonList->setCurrentIndex(currentRow);
    play();
}

/**
 * @brief MediaControl::next
 * 函数：下一曲
 */
void MediaControl::next(void)
{
    this->playlist()->next();
    currentRow = this->playlist()->currentIndex();
    qDebug()<<currentRow<<this->playlist()->currentIndex();
    if(currentRow == -1)
    {
        qDebug()<<this->playlist()->mediaCount();
        currentRow = 0;
    }
    playMedia();
}

/**
 * @brief MediaControl::getCurrentRow
 * @return currentRow
 */
int MediaControl::getCurrentRow(void) const
{
    return currentRow;
}

/**
 * @brief MediaControl::previous
 */
void MediaControl::previous()
{
    this->playlist()->previous();
    currentRow = this->playlist()->currentIndex();
    if(currentRow == -1)
    {
        qDebug()<<this->playlist()->mediaCount();
        currentRow = this->playlist()->mediaCount()-1;
    }
    playMedia();
}


void MediaControl::setCurrentRow(int row)
{
    currentRow = row;
}

void MediaControl::FTP_playMedia(void)
{
    //setUrl();
    //setMedia(url);
    FTPList->setCurrentIndex(currentRow);
    play();
}

/**
 * @brief MediaControl::setUrl
 * 在MediaControl类中访问FTP只用来在播放列表中添加相应的媒体文件
 */
void MediaControl::setUrl(void)
{
    url.setScheme("ftp");
    url.setHost(FTP_host);
    url.setPort(FTP_port);
    url.setUserName(FTP_userName);
    url.setPassword(FTP_password);
    //改成utf8即可读取
    url.setPath(FTP_mediaPath+FTP_mediaName.toUtf8());
    qDebug()<<FTP_mediaPath+FTP_mediaName.toUtf8();
}

void MediaControl::addPlayerList(void)
{
    //这个添加媒体直接加入了整个音频文件？
    locatonList->addMedia(QUrl::fromLocalFile(mediaPath+mediaName));
}

void MediaControl::FTP_addPlayerList(void)
{
    setUrl();
    FTPList->addMedia(url);
}

void MediaControl::FTP_setHost(QString host)
{
    FTP_host = host;
}
void MediaControl::FTP_setPort(int post)
{
    FTP_port = post;
}
void MediaControl::FTP_setMediaName(QString name)
{
    FTP_mediaName = name;
}
void MediaControl::FTP_setMediaPath(QString path)
{
    FTP_mediaPath = path;
}
void MediaControl::FTP_setUserName(QString userName)
{
    FTP_userName = userName;
}
void MediaControl::FTP_setPassword(QString password)
{
    FTP_password = password;
}
